package com.zz.framework.core.utils

import android.content.Context
import android.view.View
import android.widget.TextView
import androidx.annotation.StringRes
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import com.zz.framework.R
import java.util.*

/**
 * Copyright © 2020 zanezhao All rights reserved.
 * Created on 2020/3/23 6:08 PM.
 *
 * @version: V1.0
 */
class DatePickerUtils private constructor(
    callBack: TimePickerviewCallBack,
    context: Context
) {
    private var timePickerView: TimePickerView? = null
    private val callBack: TimePickerviewCallBack?
    private val mContext: Context
    //format yyyy-mm
    fun createYMPicker(@StringRes title: Int, selectedDate: Calendar = Calendar.getInstance()) {
        val builder = TimePickerBuilder(
            mContext,
            OnTimeSelectListener { date: Date, v: View? ->
                callBack?.submit(date.time)
            }
        )
            .setDividerColor(mContext.resources.getColor(R.color.colorPrimary)) //                .setContentSize(16)
            .setTextColorCenter(mContext.resources.getColor(R.color.colorPrimary)) //设置选中项的颜色
            .setDate(selectedDate)
            .setType(booleanArrayOf(true, true, false, false, false, false))
            .setLabel(mContext.getString(R.string.text_year), mContext.getString(R.string.text_month),mContext.getString(R.string.text_day), mContext.getString(R.string.text_hour), mContext.getString(R.string.text_min), mContext.getString(
                R.string.text_sec))
            .setLayoutRes(R.layout.picker_custom) { v: View ->
                v.findViewById<View>(R.id.btn_ok).setOnClickListener { view: View? ->
                    if (timePickerView != null) {
                        timePickerView!!.returnData()
                        timePickerView!!.dismiss()
                        timePickerView = null
                    }
                }
                v.findViewById<View>(R.id.btn_cancel)
                    .setOnClickListener { view: View? ->
                        if (timePickerView != null) {
                            timePickerView!!.dismiss()
                            timePickerView = null
                        }
                    }
            }
        timePickerView = builder.build()
        setTitle(title)
        timePickerView?.show()
    }

    fun createHSPicker(@StringRes title: Int, selectedDate: Calendar = Calendar.getInstance()) {
        val builder = TimePickerBuilder(
            mContext,
            OnTimeSelectListener { date: Date, v: View? ->
                callBack?.submit(date.time)
            }
        )
            .setDividerColor(mContext.resources.getColor(R.color.colorPrimary))
            //.setContentSize(16)
            .setTextColorCenter(mContext.resources.getColor(R.color.colorPrimary)) //设置选中项的颜色
            .setDate(selectedDate)
            .setType(booleanArrayOf(false, false, false, true, true, false))
            .setLabel(mContext.getString(R.string.text_year), mContext.getString(R.string.text_month),mContext.getString(R.string.text_day), mContext.getString(R.string.text_hour), mContext.getString(R.string.text_min), mContext.getString(
                R.string.text_sec))
            .setLayoutRes(R.layout.picker_custom) { v: View ->
                v.findViewById<View>(R.id.btn_ok).setOnClickListener { view: View? ->
                    if (timePickerView != null) {
                        timePickerView!!.returnData()
                        timePickerView!!.dismiss()
                        timePickerView = null
                    }
                }
                v.findViewById<View>(R.id.btn_cancel)
                    .setOnClickListener { view: View? ->
                        if (timePickerView != null) {
                            timePickerView!!.dismiss()
                            timePickerView = null
                        }
                    }
            }
        timePickerView = builder.build()
        setTitle(title)
        timePickerView?.show()
    }

    //format yyyy-mm-dd
    fun createDatePicker(
        future: Int, @StringRes title: Int,
        selectedDate: Calendar = Calendar.getInstance()
    ) {
        val builder = TimePickerBuilder(
            mContext,
            OnTimeSelectListener { date: Date, v: View? ->
                callBack?.submit(date.time)
            }
        )
            .setDividerColor(mContext.resources.getColor(R.color.colorPrimary)) //                .setContentSize(16)
            .setTextColorCenter(mContext.resources.getColor(R.color.colorPrimary)) //设置选中项的颜色
            .setDate(selectedDate)
            .setType(booleanArrayOf(true, true, true, false, false, false))
            .setLabel(mContext.getString(R.string.text_year), mContext.getString(R.string.text_month),mContext.getString(R.string.text_day), mContext.getString(R.string.text_hour), mContext.getString(R.string.text_min), mContext.getString(
                R.string.text_sec))
            .setLayoutRes(R.layout.picker_custom) { v: View ->
                v.findViewById<View>(R.id.btn_ok).setOnClickListener { view: View? ->
                    timePickerView!!.returnData()
                    timePickerView!!.dismiss()
                    timePickerView = null
                }
                v.findViewById<View>(R.id.btn_cancel)
                    .setOnClickListener { view: View? ->
                        timePickerView!!.dismiss()
                        timePickerView = null
                    }
            }
        when (future) {
            MAX_PRESENT -> builder.setRangDate(null, selectedDate)
            MIN_PRESENT -> builder.setRangDate(selectedDate, null)
            NO_LIMIT -> {
                builder.setRangDate(null, null)
            }
        }
        timePickerView = builder.build()
        setTitle(title)
        timePickerView?.show()
    }

    fun createYMDHMPicker(
        future: Int, @StringRes title: Int,
        selectedDate: Calendar = Calendar.getInstance()
    ) {
        val builder = TimePickerBuilder(
            mContext,
            OnTimeSelectListener { date: Date, v: View? ->
                callBack?.submit(date.time)
            }
        )
            .setDividerColor(mContext.resources.getColor(R.color.colorPrimary)) //                .setContentSize(16)
            .setTextColorCenter(mContext.resources.getColor(R.color.colorPrimary)) //设置选中项的颜色
            .setDate(selectedDate)
            .setType(booleanArrayOf(true, true, true, true, true, false))
            .setLabel(mContext.getString(R.string.text_year), mContext.getString(R.string.text_month),mContext.getString(R.string.text_day), mContext.getString(R.string.text_hour), mContext.getString(R.string.text_min), mContext.getString(
                            R.string.text_sec))
            .setLayoutRes(R.layout.picker_custom) { v: View ->
                v.findViewById<View>(R.id.btn_ok).setOnClickListener { view: View? ->
                    timePickerView!!.returnData()
                    timePickerView!!.dismiss()
                    timePickerView = null
                }
                v.findViewById<View>(R.id.btn_cancel)
                    .setOnClickListener { view: View? ->
                        timePickerView!!.dismiss()
                        timePickerView = null
                    }
            }
        when (future) {
            MAX_PRESENT -> builder.setRangDate(null, selectedDate)
            MIN_PRESENT -> builder.setRangDate(selectedDate, null)
            NO_LIMIT -> {
                builder.setRangDate(null, null)
            }
        }
        timePickerView = builder.build()
        setTitle(title)
        timePickerView?.show()
    }

    fun setTitle(@StringRes text: Int) {
        val textView = timePickerView!!.findViewById(R.id.tv_title) as TextView
        textView.setText(text)
    }

    interface TimePickerviewCallBack {
        fun submit(time: Long)
    }

    companion object {
        const val MAX_PRESENT = 0x11 //最大当前日期
        const val MIN_PRESENT = 0x12 //最小当前日期
        const val NO_LIMIT = 0x13 //无限制
        /**
         * 年月日
         * @param context
         * @param title
         * @param future
         * @param callBack
         */
        fun createDatePicker(context: Context, @StringRes title: Int, future: Int, callBack: TimePickerviewCallBack) {
            val dialog = DatePickerUtils(callBack, context)
            dialog.createDatePicker(future, title)
        }

        fun createYMDPicker(
            context: Context,
            @StringRes title: Int,
            future: Int,
            selectedDate: Calendar,
            callBack: TimePickerviewCallBack
        ) {
            val dialog = DatePickerUtils(callBack, context)
            dialog.createDatePicker(future, title, selectedDate)
        }

        fun createYMDPicker(context: Context, @StringRes title: Int, future: Int, callBack: TimePickerviewCallBack) {
            val dialog = DatePickerUtils(callBack, context)
            dialog.createDatePicker(future, title)
        }

        fun createYMDHMPicker(context: Context, @StringRes title: Int, future: Int, callBack: TimePickerviewCallBack) {
            val dialog = DatePickerUtils(callBack, context)
            dialog.createYMDHMPicker(future, title)
        }

        fun createHSPicker(context: Context, @StringRes title: Int, callBack: TimePickerviewCallBack) {
            val dialog = DatePickerUtils(callBack, context)
            dialog.createHSPicker(title)
        }

        fun createYMPicker(context: Context, @StringRes title: Int, callBack: TimePickerviewCallBack) {
            val dialog = DatePickerUtils(callBack, context)
            dialog.createYMPicker(title)
        }
    }

    init {
        this.callBack = callBack
        mContext = context
    }
}